<template>
  <div class="table-container">
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
        <!-- <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls">导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> -->
        <a-dropdown v-if="flag">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <HubTeamWorkTypeModal @register="registerModal" @success="handleSuccess" />
    <!-- 添加下级表单区域 -->
    <HubTeamWorkTypeModalBase @register="registerModalBase" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="emp-team-index-HubTeamWorkType" setup>
import { ref, onMounted, reactive, nextTick, watchEffect, computed, unref, watch, defineComponent } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useListPage } from '/@/hooks/system/useListPage';
import HubTeamWorkTypeModal from './components/HubTeamWorkTypeModal.vue';
import HubTeamWorkTypeModalBase from './components/HubTeamWorkTypeModalBase.vue';
import { columns, searchFormSchema } from './HubTeamWorkType.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, updateList } from './HubTeamWorkType.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { getPluraList } from '/@/api/common/api';

// 导入导出的显示与隐藏
const flag = ref<boolean>(false);
const checkedKeys = ref<Array<string | number>>([]);
//注册model
const [registerModal, { openModal }] = useModal();
const [registerModalBase, { openModal: openModalBase }] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls,  } = useListPage({
  tableProps: {
    title: 'hub_team_workpoints_work_type_config',
    api: list,
    columns,
    canResize: false,
    // dataSource: mockDate,
    showIndexColumn: false,
    rowKey: 'id',
    indentSize: 30, // 控制树形表格子表格的缩进宽度
    beforeFetch: (params) => {
      return Object.assign(params, { orgCode: orgCodeBase.value });
    }, //表格和查询条件同步更新的方法
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 180,
      fixed: 'right',
    },
    immediate: false, // 班组表格放到orgCodeBase.value拿到值之后再加载 false不会立即加载,reload()之后再加载
  },
  exportConfig: {
    name: '工作积分标准类型',
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess,
  },
});

const [registerTable, { getForm, reload, setTableData }, { rowSelection, selectedRowKeys }] = tableContext;

//在then里面修改表单结果
let orgCodeBase = ref('');
let orgNameBase = ref('');
getPluraList().then((res) => {
  res.forEach((r) => {
    r.label = r.orgName;
    r.value = r.orgCode;
  });
  orgCodeBase.value = res[0]?.value;
  orgNameBase.value = res[0]?.label;
  console.log(orgNameBase.value);
  //表格和查询条件同步更新的方法
  nextTick(() => {
    if (orgCodeBase.value) {
      reload({
        searchInfo: {
          orgCode: res[0]?.value,
        },
      });
    }
  });
  let form = getForm();
  form.updateSchema([
    {
      field: 'orgCode',
      component: 'Select',
      label: '班组',
      componentProps: {
        // more details see /src/components/Form/src/components/ApiSelect.vue
        options: res,
        onChange: (e) => {
          console.log('selected:', e);
          orgCodeBase.value = e;
          const option = res.filter((item) => item.orgCode === e);
          orgNameBase.value = option[0].orgName;
          setTableData([]);
        },
        // atfer request callback
        onOptionsChange: (options) => {
          console.log('get options', options.length, options);
        },
      },
      colProps: {
        span: 8,
      },
    },
  ]);
  form.setFieldsValue({
    orgCode: res[0]?.value,
  });
});

/**
 * 新增事件
 */
function handleAdd() {
  console.log(orgNameBase.value);
  console.log(orgCodeBase.value);
  openModal(true, {
    isUpdate: false,
    showFooter: true,
    record: {
      orgName: orgNameBase.value,
      orgCode: orgCodeBase.value,
    },
  });
}

/**
 * 新增下级事件
 */
function handleAddBase(record: Recordable) {
  openModalBase(true, {
    isUpdate: false,
    showFooter: true,
    res: {
      orgName: orgNameBase.value,
      orgCode: orgCodeBase.value,
      leaderWorkTypeName: record.workTypeName,
      leaderWorkTypeCode: record.workTypeCode,
    },
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}
/**
 * 详情
 */
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  reload();
}
/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
    // {
    //   label: '添加下级',
    //   onClick: handleAddBase.bind(null, record),
    // },
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
  ];
}
</script>

<style scoped></style>
